<%@ page import="com.the8thfold.anton.users.Roles; org.apache.tools.ant.Target" contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title>View Script</title>
    <meta name="layout" content="main" />

    <link rel="stylesheet" href="${resource(dir:'js/codemirror',file:'codemirror.css')}" />
    <link rel="stylesheet" href="${resource(dir:'js/codemirror/mode/xml',file:'xml.css')}" />

    <g:javascript library="codemirror/overlay" />
    <g:javascript library="codemirror/codemirror" />
    <g:javascript library="codemirror/mode/xml/xml" />

    <g:javascript>
        $(document).ready(function(){
        });
    </g:javascript>

</head>
<body>

<div>
    <table style="border: none;">
        <tr>
            <td width="1%"><h1>Script:</h1></td>
            <td>
                <g:formRemote onSuccess="jQuery('#${script?.scriptId}_name').effect('highlight',{color: '#90ee90'},1000);" onFailure="jQuery('#${script?.scriptId}_name').effect('highlight',{color: 'red'},1000);" name="f1" url="[controller: 'script', action: 'updateField', id: script?.scriptId]">
                    <g:hiddenField name="field" value="description"/>
                    <input id="${script?.scriptId}_name" class="ui-corner-all" type="text" name="value" value="${script?.description}" style="width: 50%;"/>
                    <a href="javascript: void();" onclick="jQuery('#f1').submit();" name="submit" class="btn save" icon="ui-icon-disk"></a>
                </g:formRemote>
            </td>
        </tr>
    </table>
    <hr/>
    <br/>
    <g:if test="${error}">
        <div class="errors">${error}</div>
        <br/>
    </g:if>
    <div class="css_right toolbar">
        <a class="btn-text inline-popupFull" title="${script?.description}" href="${createLink(controller: 'script', action: 'viewsource', id: script?.scriptId)}">View Source</a>
        <a class="btn-text" href="${createLink(controller: 'script', action: 'delete', id: script?.scriptId)}" onclick="return confirm('Are you sure?');">Delete</a>
        <sec:ifAllGranted roles="${Roles.ROLE_JOB_RUN.toString()}">
            <a class="btn-text" href="${createLink(controller: 'job', action: 'list', params: [scriptId: script?.scriptId])}">View Jobs <i>(${script?.jobs?.size()})</i></a>
        </sec:ifAllGranted>
        <sec:ifAllGranted roles="${Roles.ROLE_JOB_ADMIN.toString()}">
            <a class="btn-text" href="${createLink(controller: 'job', action: 'create', params: [scriptId: script.scriptId])}">Create Job</a>
        </sec:ifAllGranted>
    </div>
    <br/>

    <h2>Description</h2>
    <div>
        ${antProject?.getDescription()}
    </div>
    <br/>
    <table>
        <tr>
            <th>File Name</th>
            <th>Created By</th>
        </tr>
        <tr>
            %{
                String fileName = new File(script.scriptFilePath).getName()
            }%
            <td >${fileName}</td>
            <td title="${script.dateCreated}">${script?.createdBy?.firstName + " " + script?.createdBy?.lastName} (<prettytime:display date="${script.dateCreated}" />)</td>
        </tr>
    </table>

    <h2>Properties</h2>
    <div>
        <table>
            <tr>
                <th>Name</th>
                <th>Value</th>
            </tr>
            <g:each in="${projectProperties?.keySet()?.sort()}" status="propertiesIndex" var="propertyKey">
                <g:if test="${!propertyKey?.isEmpty()}">
                    <tr class="${propertiesIndex%2 ? 'odd' : 'even'}">
                        <td>${propertyKey}</td>
                        <td>${projectProperties.get(propertyKey)}</td>
                    </tr>
                </g:if>
            </g:each>
        </table>
    </div>

    <h2>Targets</h2>
    <div>
        <table>
            <tr>
                <th>Target Name</th>
                <th>Description</th>
                <th>Depends On</th>
                <th>If Condition</th>
                <th>Unless Condition</th>
                <th># Tasks</th>
            </tr>
            <g:each in="${antProject?.getTargets()?.sort()}" status="targetsIndex" var="targetEntry">
                <g:if test="${!targetEntry.key?.isEmpty()}">
                    <tr class="${targetsIndex%2 ? 'odd' : 'even'}">
                        %{
                            Target value = targetEntry.value;
                            Enumeration deps = value.getDependencies();
                            String dependenciesString = "";
                            Iterator iter = deps.iterator();
                            while (iter.hasNext()) {
                                dependenciesString = dependenciesString + " " + iter.next();
                            }
                        }%
                        <td style="${value.name.equalsIgnoreCase(antProject.getDefaultTarget()) ? 'font-weight: bold;' : ''}">${value.name}</td>
                        <td>${value.description}</td>
                        <td>${dependenciesString}</td>
                        <td>${value.if}</td>
                        <td>${value.unless}</td>
                        <td>${value.children?.size()}</td>
                    </tr>
                </g:if>
            </g:each>
        </table>
    </div>
    <div style='display:none'>
        <div id="source">
            <textarea id="code" name="code" >${contents}</textarea>
        </div>
    </div>
    <br/>
    <br/>
</div>

</body>
</html>